<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use app\common\model\LitestoreNews;
use app\common\model\LitestoreShop;
use think\Cookie;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 首页
     */
    public function index()
    {
        //是否携带code
        if(input('code')){
            cookie('user_code',input('code'));
        }

        if(cookie('user_code') && !Cookie::get('uid')){
            $this->redirect(url('user/register'));
        }

        $LitestoreNews = new LitestoreNews();
        $LitestoreShop = new LitestoreShop();

        $banner = $LitestoreNews->index_banner();
        $LitestoreShop->id = 1;

        $shop_where['city'] = $this->ip_city['id'];
        $shop_where['status'] = 1;
        $shops = $LitestoreShop->getShop_By_PosidMid()->where($shop_where)->order('weigh desc')->select();

        foreach ($shops as $key => $value) {
            //标签
            $LitestoreShop->id = $value['id'];
            if(isset($this->location['content']['point']['x']) && isset($this->location['content']['point']['y'])){
                $shops[$key]['tags'] = $LitestoreShop->getShopByTag()->select();
                //距离
                $shops[$key]['length'] = web_getDistance($this->location['content']['point']['x'],$this->location['content']['point']['y'],$value['longitude'],$value['latitude']);
            }else{
                $shops[$key]['tags'] = $shops[$key]['length'] = 12484.13;
            }

            }

        $this->assign('banner',$banner);
        $this->assign('shops',$shops);

        return $this->view->fetch();
    }

}
